﻿using AJWPFAdmin.Core.Components.AJTable.ViewModels;
using AJWPFAdmin.Core.Enums;
using AJWPFAdmin.Core.Utils;
using AJWPFAdmin.Services.EF;
using MaterialDesignExtensions.Controls;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static AJWPFAdmin.Core.ExceptionTool;
using System.Windows.Controls;
using Masuit.Tools.Models;
using EntranceGuardManager.Modules.Main.Views;
using Microsoft.EntityFrameworkCore;
using AJWPFAdmin.Core.Models.EF.Tables;
using System.Reflection;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using AJWPFAdmin.Modules.Common.Views;
using AJWPFAdmin.Services.AppointAPI;
using AJWPFAdmin.Core;

namespace EntranceGuardManager.Modules.Main.ViewModels
{

    public class ManageMeasureListViewModel : BindableBase, INavigationAware
    {

        private AJTableFetchOptions _fetchOptions;

        public AJTableFetchOptions FetchOptions
        {
            get
            {
                return _fetchOptions;
            }
            set
            {
                SetProperty(ref _fetchOptions, value);
            }
        }

        private List<AJTableColumnItem> _columns;

        public List<AJTableColumnItem> Columns
        {
            get { return _columns; }
            set { SetProperty(ref _columns, value); }
        }

        private AJTableSearchFormConfig _formConfig;

        public AJTableSearchFormConfig FormConfig
        {
            get { return _formConfig; }
            set { SetProperty(ref _formConfig, value); }
        }

        private DelegateCommand<DataGridRow> _loadingRowCmd;
        public DelegateCommand<DataGridRow> LoadingRowCmd =>
            _loadingRowCmd ?? (_loadingRowCmd = new DelegateCommand<DataGridRow>(ExecuteLoadingRowCmd));

        void ExecuteLoadingRowCmd(DataGridRow row)
        {

        }

        private IDialogService _dialogSvc;
        private DbService db;
        private AppointAPISvc _appointAPISvc;

        public ManageMeasureListViewModel(DbService dbIns, AppointAPISvc appointAPISvc, IEventAggregator eventAggregator, IDialogService dialogSvc)
        {

            _dialogSvc = dialogSvc;
            _appointAPISvc = appointAPISvc;
            //eventAggregator.GetEvent<AJGlobalExceptionResolvedEvent>().Subscribe(() => Loading = false);

            FetchOptions = new AJTableFetchOptions
            {
                //ExportFileName = "车辆记录导出",
                Handle = async (formCfg, pageCfg) =>
                {
                    var result = await _appointAPISvc.API.QueryManageMeasureListAsync(new QueryManageMeasureParams());

                    if (!result.Success)
                    {
                        ExceptionTool.FriendlyError(result.Message,$"{_appointAPISvc.API.KeyDescription} 管控措施接口提示");
                    }

                    return result.Data as AJTableFetchResult;
                }
            };

            FormConfig = new AJTableSearchFormConfig
            {
                Schemas = new ObservableCollection<AJTableSearchFormSchema>(),
                AdvFilterVisibility = System.Windows.Visibility.Collapsed,
                ExportVisibility = System.Windows.Visibility.Collapsed,
            };

            var columns = new List<AJTableColumnItem>();

            var props = typeof(ManageMeasureListItem).GetRuntimeProperties();

            foreach (var prop in props)
            {
                var attr = prop.GetCustomAttribute<DisplayAttribute>();
                if (attr == null)
                {
                    continue;
                }
                columns.Add(new AJTableColumnItem
                {
                    DataIndex = prop.Name,
                    Title = attr.Name
                });
            }

            Columns = columns;

            db = dbIns;
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            //ExecuteGetListCmd();
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return false;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

        }
    }
}
